﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using YXShop.Common;
using System.Collections.Generic;

namespace YXShop.Web.admin.store
{
    public partial class storeask_list : System.Web.UI.Page
    {
        YXShop.BLL.Store.StoreAsk bll = new YXShop.BLL.Store.StoreAsk();
        protected void Page_Load(object sender, EventArgs e)
        {
            YXShop.Common.PromptInfo.Popedom("014005001");

            if (!IsPostBack)
            {
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("id") != "")
                {

                    string types = Request["Option"].Trim();
                    string id = ChangeHope.WebPage.PageRequest.GetFormString("id");
                    if (types == "del")
                    {
                        if (YXShop.Common.PromptInfo.Message("014005003") != "ok")
                        {
                            bll.Delete(Convert.ToInt32(id));
                        }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    if (types == "delAll")
                    {
                        if (YXShop.Common.PromptInfo.Message("014005003") != "ok")
                        {
                            bll.DeleteAll(id);
                        }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    Response.End();
                    return;
                }
                this.lblMyAskList.Text = GetMyAskList("");
                this.lblMyReceiveAskList.Text = GetMyReceiveAskList("");
            }
        }

        #region 绑定
        /// <summary>
        /// 获取我发起的留言，Where前不用and
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        private string GetMyAskList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.Model.Admin.AdminInfo admin=YXShop.Common.AdministrorManager.Get();
            if (where == "")
                where = "and senderid=" + admin.AdminId + " ";
            else
                where = "and senderid=" + admin.AdminId + " and"+where;
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(where);
            table.AddHeadCol("", "<input id=\"chall\" type=\"checkbox\" onclick=\"SelectAll(chall)\" />全选");
            table.AddHeadCol("","发起者名称");
            table.AddHeadCol("","发起时间");
            table.AddHeadCol("","标题");
            table.AddHeadCol("","发送对象类型");
            table.AddHeadCol("", "回复次数");
            table.AddHeadCol("","操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp=new YXShop.Common.SysParameter();

                while (dataPage.DataReader.Read())
                {                   
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol("<input id=\"cbTm\"  type=\"checkbox\" value=\"" + dataPage.DataReader["id"].ToString() + "\" />");
                    table.AddCol(admin.AdminName);
                    table.AddCol(Convert.ToDateTime(dataPage.DataReader["sendertime"]).ToString());
                    table.AddCol(dataPage.DataReader["messagetitle"].ToString());
                    table.AddCol(YXShop.Common.ValuePair.GetStoreAskType(Convert.ToInt32(dataPage.DataReader["receivetype"].ToString())));
                    table.AddCol(Convert.ToInt32(dataPage.DataReader["answercount"]).ToString());
                    string streditUrl = YXShop.TemplateAction.Common.UrlStr("storeask_edit", "id=" + dataPage.DataReader["id"].ToString(), "admin/store/");
                    string strviewUrl = YXShop.TemplateAction.Common.UrlStr("storeask_view", "id=" + dataPage.DataReader["id"].ToString(), "admin/store/");
                    table.AddCol("<a href=\"" + strviewUrl + "\">查看</a>&nbsp;<a href=\"" + streditUrl + "\">修改</a>&nbsp;<a href=\"#\" onclick='Del(" + dataPage.DataReader["id"] + ")'>删除</a>");
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        /// <summary>
        /// 获取我接受的留言
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        private string GetMyReceiveAskList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
            string strMyReceiveWhere = "";
            if (admin.AdminType == 1)
            {
                strMyReceiveWhere = " and receivetype=1";
            }
            else if (admin.AdminType == 2)
            {
                YXShop.BLL.Admin.Administrators adminBLL = new BLL.Admin.Administrators();
                List<YXShop.Model.Store.StoreCenter> storecenters = adminBLL.GetStoreCenterAdmin_StoreCenter(admin.AdminId);
                List<int> storeCenterids = new List<int>();
                foreach (YXShop.Model.Store.StoreCenter sc in storecenters)
                    storeCenterids.Add(sc.Sid.Value);
                strMyReceiveWhere = " and receivetype=2" + ChangeHope.Common.Util.GetWhereInClause(storeCenterids, "receivestorecenterid") + " ";
            }
            else if (admin.AdminType == 3)
            {
                YXShop.BLL.Admin.Administrators adminBLL = new BLL.Admin.Administrators();
                List<YXShop.Model.Store.Store> stores = adminBLL.GetStoreAdmin_Store(admin.AdminId);
                List<int> storeids = new List<int>();
                foreach (YXShop.Model.Store.Store s in stores)
                    storeids.Add(s.Sid.Value);
                strMyReceiveWhere = " and receivetype=3" + ChangeHope.Common.Util.GetWhereInClause(storeids, "receivestoreid") + " ";

            }
            if (where == "")
                where = strMyReceiveWhere;
            else
                where = strMyReceiveWhere + " and" + where;
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(where);
            table.AddHeadCol("", "发起者名称");
            table.AddHeadCol("", "发起时间");
            table.AddHeadCol("", "标题");
            table.AddHeadCol("", "发送对象类型");
            table.AddHeadCol("", "回复次数");
            table.AddHeadCol("", "操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    YXShop.BLL.Admin.Administrators adminuserBll = new YXShop.BLL.Admin.Administrators();
                    YXShop.Model.Admin.Administrators adminuser = adminuserBll.GetModel(Convert.ToInt32(dataPage.DataReader["senderid"]));
                    table.AddCol(adminuser.Name);
                    table.AddCol(Convert.ToDateTime(dataPage.DataReader["sendertime"]).ToString());
                    table.AddCol(dataPage.DataReader["messagetitle"].ToString());
                    table.AddCol(YXShop.Common.ValuePair.GetStoreAskType(Convert.ToInt32(dataPage.DataReader["receivetype"].ToString())));
                    table.AddCol(Convert.ToInt32(dataPage.DataReader["answercount"]).ToString());
                    string strviewUrl = YXShop.TemplateAction.Common.UrlStr("storeask_view", "id=" + dataPage.DataReader["id"].ToString(), "admin/store/");
                    table.AddCol("<a href=\"" + strviewUrl + "\">查看</a>");
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        #endregion

        #region 查询
        protected void linkQuery_Click(object sender, EventArgs e)
        {
            string strWhere="";
            this.lblMyAskList.Text = GetMyAskList(strWhere);
            this.lblMyReceiveAskList.Text = GetMyReceiveAskList(strWhere);
        }
        #endregion

        #region 获取查询条件
        #endregion
    }
}
